package com.example.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.demo.R;
import com.example.demo.data.model.News;
import com.example.demo.data.model.NewsAdapter;
import com.example.demo.data.model.NewsList;
import com.example.demo.databinding.ActivityNewsListBinding;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

public class NewsListActivity extends AppCompatActivity {


    ActivityNewsListBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNewsListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ListView listView = binding.newsList;

        NewsAdapter adapter = new NewsAdapter(NewsListActivity.this, R.layout.news_item, NewsList.newsList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            News news = NewsList.newsList.get(i);
            Intent intent = new Intent();
            intent.putExtra("title",news.getTitle());
            intent.setClass(this, NewsDetailActivity.class);
            startActivity(intent);
        });

        NewsList.init(adapter);

        SmartRefreshLayout refreshLayout = findViewById(R.id.news_main_layout);
        refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            refreshlayout.finishRefresh(1000);
            NewsList.init(adapter);
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            refreshlayout.finishLoadMore(1000);
            NewsList.addPage(adapter);
        });

        setJump();

    }

    private void setJump(){
        binding.jumpToAstro.setOnClickListener(view -> {
            startActivity(new Intent(this, AstroActivity.class));
        });
        binding.jumpToHis.setOnClickListener(view -> {
            startActivity(new Intent(this, DayInHistoryActivity.class));
        });
        binding.jumpToGold.setOnClickListener(view -> {
            startActivity(new Intent(this, GoldActivity.class));
        });
        binding.jumpToNews.setOnClickListener(view -> {
            startActivity(new Intent(this, NewsListActivity.class));
        });
        binding.jumpToPhone.setOnClickListener(view -> {
            startActivity(new Intent(this, PhoneActivity.class));
        });
    }
}